cd ${work}5_reg_analysis

*TABLES 6 AND A4 ARE PRODUCED IN A SINGLE TABLE 

local ylist		"log_quantity log_price log_mc log_mu log_quality ihs_var_profits_nos ihs_profits" 
local felist	`" "IY FP" "IY FP FY" "PY FP FY" "'
local wlist		`" "c.sales##c.sales##c.sales##c.sales" "" "" "'
local x			"exp dest dest_sq"

use temp\regsample_ipt, clear

eststo clear
local col=1	
foreach fe of local felist {
    
	local row=1
	local w : word `col' of `wlist'
	local estlist1
	local estlist2
	
foreach y of local ylist {
    
	cap gen x_`row'=.
	replace x_`row'=treated_2
	lab var x_`row' "`:var lab `y''"
	local x_`row'
	foreach v of local x {
		cap gen `v'_`row'=`v'
		lab var `v'_`row' "`:var lab `v''"
		local x_`row' `x_`row'' `v'_`row'
	}	
	eststo est`row' : reghdfe `y' x_`row' `x_`row'' `w', a(`fe') vce(cl F) nosamp
	local estlist1 `estlist1' est`row'
	local ++row

	cap gen x_`row'=.
	replace x_`row'=treated_2
	lab var x_`row' "`:var lab `y''"
	local x_`row'
	foreach v of local x {
		cap gen `v'_`row'=`v'
		lab var `v'_`row' "`:var lab `v''"
		local x_`row' `x_`row'' `v'_`row'
	}		
	eststo est`row' : ivreghdfe `y' (x_`row'=tdcinitiative_2) `x_`row'' `w', a(`fe') cl(F)
	local estlist2 `estlist2' est`row'
	local ++row
	local N = e(N)
	local F = e(widstat)
	
}
	
	eststo stacked_`col' : appendmodels `estlist1' `estlist2'
	eststo drop est*
	qui estadd scalar N `N'
	qui estadd scalar F `F'
	qui estadd local FE `fe'
	if "`w'"!=""	qui estadd local SC "Yes"
	if "`w'"==""	qui estadd local SC "No"
	if "`x'"=="" qui estadd local EC "No"
	if "`x'"!="" qui estadd local EC "Yes"

	local ++col
	
}	
	
esttab using output\table_6_a4.tex, replace booktabs se label nomtitles ///
	title("Effects of Export Support on Firm-Product-Year Outcomes") star(* 0.1 ** 0.05 *** 0.01) keep(x_*) ///
	stats(N F FE SC EC , labels("Observations" "IV F-stat" "Fixed Effects" "Size Control" "Export Controls") fmt(%10.0fc %10.0fc "" "" "")) refcat(x_1 "OLS" x_2 "2SLS", nolabel)
	

local ylist		"log_quantity log_price log_mc log_mu log_quality ihs_var_profits_nos ihs_profits"	
local keeplist
local lablist
local n=2
foreach y of local ylist {
	local keeplist "`keeplist' exp_`n' dest_`n' dest_sq_`n'"
	local lab : var lab `y'
	local lablist `"`lablist' exp_`n' "\textit{`lab'}" "'
	local ++n
	local ++n
}	
disp "`keeplist'"
disp `"`lablist'"'
	
esttab using output\table_a5.tex, replace booktabs se label nomtitles ///
	title("2SLS Effects of Export Support on Firm-Product-Year Outcomes; Export Controls") star(* 0.1 ** 0.05 *** 0.01) keep(`keeplist') ///
	stats(N F FE SC EC , labels("Observations" "IV F-stat" "Fixed Effects" "Size Control" "Export Controls") fmt(%10.0fc %10.0fc "" "" "")) refcat(`lablist', nolabel)	